/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package entities;

import company.search.interfaces.SearchRequerimetsOR;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author damfercer
 */
@Entity
@Table(name = "search_interest_area_requeriment_or_operator")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "SearchInterestAreaRequerimentOrOperator.findAll", query = "SELECT s FROM SearchInterestAreaRequerimentOrOperator s"),
    @NamedQuery(name = "SearchInterestAreaRequerimentOrOperator.findByIdSearchInterestAreaRequerimentOrOperator", query = "SELECT s FROM SearchInterestAreaRequerimentOrOperator s WHERE s.idSearchInterestAreaRequerimentOrOperator = :idSearchInterestAreaRequerimentOrOperator")})
public class SearchInterestAreaRequerimentOrOperator implements Serializable, SearchRequerimetsOR {
    @Basic(optional = false)
    @NotNull
    @Column(name = "IMPORTANCE")
    private int importance;
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_SEARCH_INTEREST_AREA_REQUERIMENT_OR_OPERATOR")
    private Integer idSearchInterestAreaRequerimentOrOperator;
    @JoinColumn(name = "JOB_POSITION", referencedColumnName = "ID_JOB_POSITION")
    @ManyToOne(fetch = FetchType.LAZY)
    private JobPosition jobPosition;
    @JoinColumn(name = "SECTOR", referencedColumnName = "ID_SECTOR")
    @ManyToOne(fetch = FetchType.LAZY)
    private Sector sector;
    @JoinColumn(name = "SEARCH", referencedColumnName = "ID_SEARCH")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Search search;
    @JoinColumn(name = "MAXIMUM_DESIRED_SALARY", referencedColumnName = "ID_DESIRED_SALARY")
    @ManyToOne(fetch = FetchType.LAZY)
    private DesiredSalary maximumDesiredSalary;

    public SearchInterestAreaRequerimentOrOperator() {
    }

    public SearchInterestAreaRequerimentOrOperator(Integer idSearchInterestAreaRequerimentOrOperator) {
        this.idSearchInterestAreaRequerimentOrOperator = idSearchInterestAreaRequerimentOrOperator;
    }

    public Integer getIdSearchInterestAreaRequerimentOrOperator() {
        return idSearchInterestAreaRequerimentOrOperator;
    }

    public void setIdSearchInterestAreaRequerimentOrOperator(Integer idSearchInterestAreaRequerimentOrOperator) {
        this.idSearchInterestAreaRequerimentOrOperator = idSearchInterestAreaRequerimentOrOperator;
    }

    public JobPosition getJobPosition() {
        return jobPosition;
    }

    public void setJobPosition(JobPosition jobPosition) {
        this.jobPosition = jobPosition;
    }

    public Sector getSector() {
        return sector;
    }

    public void setSector(Sector sector) {
        this.sector = sector;
    }

    public Search getSearch() {
        return search;
    }

    public void setSearch(Search search) {
        this.search = search;
    }

    public DesiredSalary getMaximumDesiredSalary() {
        return maximumDesiredSalary;
    }

    public void setMaximumDesiredSalary(DesiredSalary maximumDesiredSalary) {
        this.maximumDesiredSalary = maximumDesiredSalary;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idSearchInterestAreaRequerimentOrOperator != null ? idSearchInterestAreaRequerimentOrOperator.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        boolean sectorRes=false;
        boolean maximumDesiredSalaryRes=false;
        boolean jobPositionRes=false;
        if (!(object instanceof SearchInterestAreaRequerimentOrOperator)) {
            return false;
        }
        SearchInterestAreaRequerimentOrOperator other = (SearchInterestAreaRequerimentOrOperator) object;
        
        //Sector test
        if(this.sector==null && other.getSector()==null){
            sectorRes=true;
        }
        else if(this.sector!=null && other.getSector()!=null){
            sectorRes=this.sector.equals(other.getSector());
        }
        //MaximumDesiredSalary test
        if(this.getMaximumDesiredSalary()==null && other.getMaximumDesiredSalary()==null){
            maximumDesiredSalaryRes=true;
        }
        else if(this.getMaximumDesiredSalary()!=null && other.getMaximumDesiredSalary()!=null){
            maximumDesiredSalaryRes=this.getMaximumDesiredSalary().equals(other.getMaximumDesiredSalary());
        }
        //JobPosition test
        if(this.getJobPosition()==null && other.getJobPosition()==null){
            jobPositionRes=true;
        }
        else if(this.getJobPosition()!=null && other.getJobPosition()!=null){
            jobPositionRes=this.getJobPosition().equals(other.getJobPosition());
        }
        Boolean res=sectorRes && maximumDesiredSalaryRes && jobPositionRes;
        return res;
    }

    @Override
    public String toString() {
        return "entities.SearchInterestAreaRequerimentOrOperator[ idSearchInterestAreaRequerimentOrOperator=" + idSearchInterestAreaRequerimentOrOperator + " ]";
    }

    public int getImportance() {
        return importance;
    }

    public void setImportance(int importance) {
        this.importance = importance;
    }

    @Override
    public Integer getCandidateTotalPoints(Candidate c) {
        Integer finalPoints=0;
        Integer points;
        Integer salaryPoints=20;
        Integer jobPositionPoints=12;
        Integer sectorPoints=40;
        Integer requeriments=0;
        Integer candidateRequeriments;
        if(c.getCandidateInterestAreaList()!=null && !c.getCandidateInterestAreaList().isEmpty()){
            if(getSector()!=null){
                requeriments++;
            }
            if(getMaximumDesiredSalary()!=null){
                requeriments++;
            }
            if(getJobPosition()!=null){
                requeriments++;
            }
            for(CandidateInterestArea candidateInterestArea:c.getCandidateInterestAreaList()){
                candidateRequeriments=0;
                points=0;
                if(getMaximumDesiredSalary()!=null){
                    if(candidateInterestArea.getDesiredSalary()!=null && candidateInterestArea.getDesiredSalary().getValue()>=getMaximumDesiredSalary().getValue()){
                        points+=(salaryPoints*importance);
                        candidateRequeriments++;
                    }
                }
                if(getSector()!=null){
                    if(candidateInterestArea.getSector()!=null && candidateInterestArea.getSector().equals(getSector())){
                        points+=(sectorPoints*importance);
                        candidateRequeriments++;
                    }
                }
                if(getJobPosition()!=null){
                    if(candidateInterestArea.getJobPosition()!=null && candidateInterestArea.getJobPosition().equals(getJobPosition())){
                        points+=(jobPositionPoints*importance);
                        candidateRequeriments++;
                    }
                }
                if(requeriments<=candidateRequeriments){
                    points=points*2;
                }
                finalPoints+=points;
            }
        }
        return finalPoints;
    }
    
}
